package org.boun.swe.ARCEONE.Utility;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class StorageManager {
	private SharedPreferences sharedPreferences;
	private SharedPreferences.Editor sharedPreferencesEditor;
	
	public StorageManager(Context context){
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        sharedPreferencesEditor = sharedPreferences.edit();
	}
	
	public Object getStoredData(String key, Class<?> clazz){
		Object obj = null;
		String className = clazz.getName();
		
		if ("java.lang.String".equals(className))
			obj = sharedPreferences.getString(key, "");
		else if ("java.lang.Integer".equals(className))
			obj = sharedPreferences.getInt(key, 0);
		else if ("java.lang.Boolean".equals(className))
			obj = sharedPreferences.getBoolean(key, false);
		else if ("java.lang.Long".equals(className))
			obj = sharedPreferences.getLong(key, 0);
		else if ("java.lang.Float".equals(className))
			obj = sharedPreferences.getFloat(key, 0.0f);
		
		return obj;
	}
	
	public boolean storeData(String key, Object data, Class<?> clazz){
		String className = clazz.getName();
		
		if ("java.lang.String".equals(className))
			sharedPreferencesEditor.putString(key, (String)data);
		else if ("java.lang.Integer".equals(className))
			sharedPreferencesEditor.putInt(key, (Integer)data);
		else if ("java.lang.Boolean".equals(className))
			sharedPreferencesEditor.putBoolean(key, (Boolean)data);
		else if ("java.lang.Long".equals(className))
			sharedPreferencesEditor.putLong(key, (Long)data);
		else if ("java.lang.Float".equals(className))
			sharedPreferencesEditor.putFloat(key, (Float)data);
		
        return sharedPreferencesEditor.commit();
	}
	
	public boolean clearStoredData(){
		sharedPreferencesEditor.clear();
		return sharedPreferencesEditor.commit();
	}
}
